/**
 * 桩函数删除接口测试用例
 *
 * Created by snowingsea on 2020/03/07.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
const randomstring = require('randomstring');
chai.use(require('chai-http'));
const common = require('./../../../common');
const Administrator = require('./../../../../models/administrator');
const uri = require('./../../uri');
const benchmark = require('./../../../../benchmark/analyse-files/logic_bombs');

module.exports = describe('/DELETE functions/:id/stubs/stubName.json', function () {
  const administrator = new Administrator();
  const newUserIds = [];
  let projectVersion = {};
  let functionId;
  let func = {};
  let unmodifiedStubName;

  function url(functionId, stubName) {
    return `/functions/${functionId}/stubs/${stubName}.json`;
  }
  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((project) => project.createRandomVersion())
      .then((res) => {
        projectVersion = res;
        return projectVersion.getAllStubs(benchmark);
      })
      .then(([stub]) => {
        functionId = stub.functionId;
        unmodifiedStubName = stub.stubName;
        return projectVersion.generateSimpleFunctionModel(functionId, '', '');
      })
      .then((f) => {
        func = f;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });
  common.should.be.user(uri, url, 'delete');
  common.should.be.tester(uri, url, 'delete');

  const sampleAddStub = {
    stubName: randomstring.generate({ length: 16, charset: 'alphabetic' }),
    returnType: 'int',
    paramTypeNames: ['int', 'int', 'int'],
    paramNames: ['1', '2', '3'],
    times: '7',
  };

  // 正常删除
  it('it should DELETE stub sccessed', function (done) {
    func.addStub(sampleAddStub)
      .then(() => projectVersion.project.creator.login())
      .then(() => {
        chai.request(uri)
          .delete(url(func.functionId, sampleAddStub.stubName))
          .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);
            done();
          });
      })
      .catch(done);
  });

  // 无法删除不可编辑的桩函数
  it('it should not delete stub not modifiable', function (done) {
    projectVersion.project.creator.login()
      .then(() => {
        chai.request(uri)
          .delete(url(func.functionId, unmodifiedStubName))
          .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(416);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可删除不存在的函数的桩函数
  it('it should fail to delete stub when the function is not exists', function (done) {
    const stubName = randomstring.generate({ length: 16, charset: 'alphabetic' });
    new Promise((resolve) => {
      chai.request(uri)
        .delete(url('000000000000000000000000', stubName))
        .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
        .end((err, res) => {
          res.should.have.status(404);
          common.should.be.errorMessage(res.body);
          resolve();
        });
    })
      .then(done)
      .catch(done);
  });

  // 不可删除他人项目的桩函数
  it('it should fail to delete stub when the project is not yours', function (done) {
    let newUser;
    const stubName = randomstring.generate({ length: 16, charset: 'alphabetic' });
    administrator.createRandomTester()
      .then((tester) => {
        newUser = tester;
        newUserIds.push(tester.userId);
        return tester.getToken();
      })
      .then(() => {
        chai.request(uri)
          .delete(url(func.functionId, stubName))
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可删除不存在的桩函数
  it('it should not delete stub not exists', function (done) {
    chai.request(uri)
      .delete(url(func.functionId, sampleAddStub.stubName))
      .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
      .end((err, res) => {
        res.should.have.status(404);
        common.should.be.errorMessage(res.body);
        done();
      });
  });
});
